<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class ArticleController extends Controller
{

    public function list(Request $request)
    {
        try {
            $param = $request->all();
            $lists = Article::list($param, ['*'], 8);//SHAPE_TEXT
            $this->stateToWord($lists, [
                'street' => Article::STREET_TEXT,
                'special' => Article::SPECIAL_TEXT,
                'nature' => Article::NATURE_TEXT,
                'form' => Article::FORM_TEXT,
                'sfpe' => Article::SFPE_TEXT,
                'maintenance' => Article::MAINTENANCE_TEXT,
                'fund' => Article::FUND_TEXT,
                'rainshed' => Article::EAINSHED_TEXT,
                'wall' => Article::WALL_TEXT,
                'bicycle' => Article::BICYCLE_TEXT,
                'channel' => Article::CHANNEL_TEXT,
                'speak' => Article::SPEAK_TEXT,
                'fire' => Article::FIRE_TEXT,
                'fire_use' => Article::FIRE_USE_TEXT,
                'hydrant' => Article::HYDRANT_TEXT,
                'hydrant_use' => Article::HYDRANT_USE_TEXT,
                'whydrant' => Article::WHYDRANT_TEXT,
                'whydrant_use' => Article::WHYDRANT_USE_TEXT,
                'outfire' => Article::OUTFIRE_TEXT,
                'outfire_use' => Article::OUTFIRE_USE_TEXT,
                'smoke' => Article::SMOKE_TEXT,
                'smoke_use' => Article::SMOKE_USE_TEXT,
                'shutter' => Article::SHUTTER_TEXT,
                'shutter_use' => Article::SHUTTER_USE_TEXT,
                'gas' => Article::GAS_TEXT,
                'gas_use' => Article::GAS_USE_TEXT,
                'keynote' => Article::KEYNOTE_TEXT,
                'warm' => Article::WARM_TEXT,
                'hidden1' => Article::HIDDEN1_TEXT,
                'hidden2' => Article::HIDDEN2_TEXT,
                'hidden3' => Article::HIDDEN3_TEXT,
                'hidden4' => Article::HIDDEN4_TEXT,
                'hidden5' => Article::HIDDEN5_TEXT,
                'hidden6' => Article::HIDDEN6_TEXT,
                'hidden7' => Article::HIDDEN7_TEXT,
                'hidden8' => Article::HIDDEN8_TEXT,
                'hidden9' => Article::HIDDEN9_TEXT,
                'hidden10' => Article::HIDDEN10_TEXT,
                'hidden11' => Article::HIDDEN11_TEXT,
                'hidden12' => Article::HIDDEN12_TEXT,
                'hidden13' => Article::HIDDEN13_TEXT,
                'hidden14' => Article::HIDDEN14_TEXT,
            ]);
            $users = User::select(['id','username'])->orderBy('id', 'desc')->get()->toArray();
            $search = [
                'users' => $users,
                'streets' => Article::STREET_TEXT,
                'specials' => Article::SPECIAL_TEXT,
                'natures' => Article::NATURE_TEXT,
                'shapes' => Article::SHAPE_TEXT,
                'forms' => Article::FORM_TEXT,
                'sfpes' => Article::SFPE_TEXT,
                'maintenances' => Article::MAINTENANCE_TEXT,
                'funds' => Article::FUND_TEXT,
                'rainsheds' => Article::EAINSHED_TEXT,
                'walls' => Article::WALL_TEXT,
                'bicycles' => Article::BICYCLE_TEXT,
                'signs' => Article::SIGN_TEXT,
                'channels' => Article::CHANNEL_TEXT,
                'speaks' => Article::SPEAK_TEXT,
                'fires' => Article::FIRE_TEXT,
                'fire_uses' => Article::FIRE_USE_TEXT,
                'hydrants' => Article::HYDRANT_TEXT,
                'hydrant_uses' => Article::HYDRANT_USE_TEXT,
                'whydrants' => Article::WHYDRANT_TEXT,
                'whydrant_uses' => Article::WHYDRANT_USE_TEXT,
                'outfires' => Article::OUTFIRE_TEXT,
                'outfire_uses' => Article::OUTFIRE_USE_TEXT,
                'smokes' => Article::SMOKE_TEXT,
                'smoke_uses' => Article::SMOKE_USE_TEXT,
                'shutters' => Article::SHUTTER_TEXT,
                'shutter_uses' => Article::SHUTTER_USE_TEXT,
                'gass' => Article::GAS_TEXT,
                'gas_uses' => Article::GAS_USE_TEXT,
                'keynotes' => Article::KEYNOTE_TEXT,
                'warms' => Article::WARM_TEXT,
                'hidden1s' => Article::HIDDEN1_TEXT,
                'hidden2s' => Article::HIDDEN2_TEXT,
                'hidden3s' => Article::HIDDEN3_TEXT,
                'hidden4s' => Article::HIDDEN4_TEXT,
                'hidden5s' => Article::HIDDEN5_TEXT,
                'hidden6s' => Article::HIDDEN6_TEXT,
                'hidden7s' => Article::HIDDEN7_TEXT,
                'hidden8s' => Article::HIDDEN8_TEXT,
                'hidden9s' => Article::HIDDEN9_TEXT,
                'hidden10s' => Article::HIDDEN10_TEXT,
                'hidden11s' => Article::HIDDEN11_TEXT,
                'hidden12s' => Article::HIDDEN12_TEXT,
                'hidden13s' => Article::HIDDEN13_TEXT,
                'hidden14s' => Article::HIDDEN14_TEXT,
            ];

            return $this->returnJson(0, ['lists' => $lists, 'search' => $search]);
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, $e->getMessage());
        }
    }



    public function info(Request $request)
    {
        try {
            $id = $request->get('id', 0);
            if (empty($id)) {
                return $this->returnJson(1, null, 'ID参数必须存在');
            }
            $info = Article::findOrFail($id);
            if(!$info){
                return $this->returnJson(1, null, '数据不存在');
            }
            $info = [$info->toArray()];
            $this->stateToWord($info, [
                'street' => Article::STREET_TEXT,
                'special' => Article::SPECIAL_TEXT,
                'nature' => Article::NATURE_TEXT,
                'form' => Article::FORM_TEXT,
                'sfpe' => Article::SFPE_TEXT,
                'maintenance' => Article::MAINTENANCE_TEXT,
                'fund' => Article::FUND_TEXT,
                'rainshed' => Article::EAINSHED_TEXT,
                'wall' => Article::WALL_TEXT,
                'bicycle' => Article::BICYCLE_TEXT,
                'channel' => Article::CHANNEL_TEXT,
                'sign' => Article::SIGN_TEXT,
                'speak' => Article::SPEAK_TEXT,
                'fire' => Article::FIRE_TEXT,
                'fire_use' => Article::FIRE_USE_TEXT,
                'hydrant' => Article::HYDRANT_TEXT,
                'hydrant_use' => Article::HYDRANT_USE_TEXT,
                'whydrant' => Article::WHYDRANT_TEXT,
                'whydrant_use' => Article::WHYDRANT_USE_TEXT,
                'outfire' => Article::OUTFIRE_TEXT,
                'outfire_use' => Article::OUTFIRE_USE_TEXT,
                'smoke' => Article::SMOKE_TEXT,
                'smoke_use' => Article::SMOKE_USE_TEXT,
                'shutter' => Article::SHUTTER_TEXT,
                'shutter_use' => Article::SHUTTER_USE_TEXT,
                'gas' => Article::GAS_TEXT,
                'gas_use' => Article::GAS_USE_TEXT,
                'keynote' => Article::KEYNOTE_TEXT,
                'warm' => Article::WARM_TEXT,
                'hidden1' => Article::HIDDEN1_TEXT,
                'hidden2' => Article::HIDDEN2_TEXT,
                'hidden3' => Article::HIDDEN3_TEXT,
                'hidden4' => Article::HIDDEN4_TEXT,
                'hidden5' => Article::HIDDEN5_TEXT,
                'hidden6' => Article::HIDDEN6_TEXT,
                'hidden7' => Article::HIDDEN7_TEXT,
                'hidden8' => Article::HIDDEN8_TEXT,
                'hidden9' => Article::HIDDEN9_TEXT,
                'hidden10' => Article::HIDDEN10_TEXT,
                'hidden11' => Article::HIDDEN11_TEXT,
                'hidden12' => Article::HIDDEN12_TEXT,
                'hidden13' => Article::HIDDEN13_TEXT,
                'hidden14' => Article::HIDDEN14_TEXT,
            ]);
            $shapes = Article::SHAPE_TEXT;
            return $this->returnJson(0, ['info' => $info[0], 'shapes' => $shapes]);
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, $e->getMessage());
        }
    }

    public function count(Request $request){
        try {
//            $id = $request->get('id', 0);
            $count = Article::select(\DB::raw(
                'count(*) as `total_count`,
                sum(IF(`fund` = 2, 1, 0)) as `fund_count`,
                sum(IF(`rainshed` = 1, 1, 0)) as `rainshed_count`,
                sum(IF(`bicycle` = 2, 1, 0)) as `bicycle_count`,
                sum(IF(`sign` = 2, 1, 0)) as `sign_count`,
                sum(IF(`channel` = 2, 1, 0)) as `channel_count`,
                sum(IF(`speak` = 2, 1, 0)) as `speak_count`,
                sum(IF(`hidden1` = 2, 1, 0)) as `hidden1_count`,
                sum(IF(`hidden2` = 2, 1, 0)) as `hidden2_count`,
                sum(IF(`hidden3` = 2, 1, 0)) as `hidden3_count`,
                sum(IF(`hidden4` = 2, 1, 0)) as `hidden4_count`,
                sum(IF(`hidden5` = 2, 1, 0)) as `hidden5_count`,
                sum(IF(`hidden6` = 2, 1, 0)) as `hidden6_count`,
                sum(IF(`hidden7` = 2, 1, 0)) as `hidden7_count`,
                sum(IF(`hidden8` = 2, 1, 0)) as `hidden8_count`,
                sum(IF(`hidden9` = 2, 1, 0)) as `hidden9_count`,
                sum(IF(`hidden10` = 2, 1, 0)) as `hidden10_count`,
                sum(IF(`hidden11` = 2, 1, 0)) as `hidden11_count`,
                sum(IF(`hidden12` = 2, 1, 0)) as `hidden12_count`,
                sum(IF(`hidden13` = 2, 1, 0)) as `hidden13_count`,
                sum(IF(`hidden14` = 2, 1, 0)) as `hidden14_count`
               '))->get();
            return $this->returnJson(0,$count[0]);
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, $e->getMessage());
        }
    }

    public function chart(){
        try {
            $count = Article::select(\DB::raw('count(`id`) as `mumber`, `street`'))->groupBy('street')->orderBy('mumber','desc')->get()->toArray();
            $street = Article::STREET_TEXT;
            $arr = [];
            foreach ($count as $k => $val){
                if(array_key_exists($val['street'], $street)){
                    $arr[] = $val['street'];
                }
            }
            foreach ($street as $key => $value){
                if(!in_array($key, $arr)){
                    $count[] =  ['mumber' => 0, 'street' => $key];
                }
            }
            $this->stateToWord($count, [
                'street' => $street,
            ]);
            return $this->returnJson(0, $count);
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, $e->getMessage());
        }

    }



    public function destroy(Request $request)
    {
        try {
            $id = $request->get('id', 0);
            if (empty($id)) {
                return $this->returnJson(1, null, 'ID参数必须存在');
            }
            $info = Article::findOrFail($id);
            if (empty($info)) {
                return $this->returnJson(1, null, '您要操作的信息不存在');
            }
            Article::destroy($id);
            return $this->returnJson();
        } catch (\Throwable $e) {
            return $this->returnJson(1, null, $e->getMessage());
        }
    }
}
